/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.generator.pages;

import net.db.generator.field.models.BaseFieldModel;
import net.db.generator.utils.ClassGeneratorUtil;

/**
 *
 * @author abosancic
 */
public class JSFEditWriter extends JSFBaseWriter{

    public JSFEditWriter() {
        fileName = "Edit";
    }

    @Override
    protected void generateDefineTitle() {
        stringXHTML.append(spaces).append("<ui:define name=\"title\">\n")
                .append(spaces).append(spaces).append("<h:outputText value=\"#{bundle.Edit" + ClassGeneratorUtil.humanize(className) + "Title}\"></h:outputText>\n")
                .append(spaces).append("</ui:define>").append("\n");
        // add Bundle
        properties.put("Edit" + ClassGeneratorUtil.humanize(className) + "Title", "Edit " + ClassGeneratorUtil.humanize(className));
    }
    
    @Override
    protected void generateFields() {
        StringBuilder toStringMethod = new StringBuilder();
        
        for (BaseFieldModel fieldModel : fields) {
            String field = fieldModel.getFieldName();
            field = ClassGeneratorUtil.humanizeToField(fieldModel.getFieldName());
            stringXHTML.append(spaces).append(spaces).append("<h:outputLabel value=\"#{bundle.Edit" + ClassGeneratorUtil.humanize(className) + "Label_" + field + "}\" for=\"" + field + "\" />\n")
                    .append(spaces).append(spaces).append("<h:inputText id=\"" + field + "\" value=\"#{"  + ClassGeneratorUtil.humanizeToField(className) +  "Controller.selected."  + field +  "}\" title=\"#{bundle.Edit" + ClassGeneratorUtil.humanize(className) + "Title_" + field + "}\" "
                    + "required=\"true\" requiredMessage=\"#{bundle.Edit" + ClassGeneratorUtil.humanize(className) + "RequiredMessage_" + field + "}\"/>");
            stringXHTML.append("\n");
            stringXHTML.append("<br /> \n");
            // add Bundle
            properties.put("Edit" + ClassGeneratorUtil.humanize(className) + "Label_" + field, ClassGeneratorUtil.humanize(field) + ":");
            properties.put("Edit" + ClassGeneratorUtil.humanize(className) + "Title_" + field, ClassGeneratorUtil.humanize(field));
            properties.put("Edit" + ClassGeneratorUtil.humanize(className) + "RequiredMessage_" + field, "The " +  ClassGeneratorUtil.humanize(field) + " field is required.");
        }
        
        stringXHTML.append(spaces).append("\n");
    }
    
    @Override
     protected void generateEndForm() {
        stringXHTML.append(spaces).append(" </h:panelGroup>\n")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.update}\" "
                + "value=\"#{bundle.Edit" + ClassGeneratorUtil.humanize(className) + "SaveLink}\" />\n")
                .append(spaces).append(spaces).append("<br />\n")
                .append(spaces).append(spaces).append("<br />\n")
                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareList}\" value=\"#{bundle.Create" + ClassGeneratorUtil.humanize(className) + "ShowAllLink}\" immediate=\"true\"/>\n")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:link outcome=\"/index\" value=\"#{bundle.Create" + ClassGeneratorUtil.humanize(className) + "IndexLink}\"/>\n")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareList}\" value=\"#{bundle.Edit" + ClassGeneratorUtil.humanize(className) + "ShowAllLink}\" immediate=\"true\"/>")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:link outcome=\"/index\" value=\"#{bundle.Edit" + ClassGeneratorUtil.humanize(className) + "IndexLink}\" />")
                .append(spaces).append("</h:form>\n");
    }
    
    @Override
    protected void addInitProperties() {
        String name = ClassGeneratorUtil.humanize(className);
        properties.put("Edit" + name + "SaveLink", "Save");
        properties.put("Edit" + name + "ViewLink", "View");
        properties.put("Edit" + name + "ShowAllLink", "Show All " + name + " Items");
        properties.put("Edit" + name + "IndexLink", "Index");
    }
}
